package MouseControl;

import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

/**
 * Class DefaultMouseControler
 */
public class DefaultMouseControler implements MouseControlerInterface,
		MouseMotionListener {

	//
	// Fields
	//
	Robot robot;
	int x = 0;
	int y = 0;

	//
	// Constructors
	//
	public DefaultMouseControler() {
		try {
			robot = new Robot();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.toString());
		}
	};

	//
	// Methods
	//

	//
	// Accessor methods
	//

	//
	// Other methods
	//

	/**
	 *
	 * 0=OK 1=edge colision
	 *
	 * @return int
	 * @param x
	 * @param y
	 */
	public int moveCursor(int x2, int y2) {
		x = x2;
		y = y2;
		if (x >= 1680) {
			x = 1680;
		}
		if (y >= 1040) {
			y = 1040;
		}

		robot.mouseMove(x, y);
		return 0;
	}

	/**
	 *
	 * 0=OK 1=other case
	 *
	 * @return int
	 */
	public int pressLeft() {
		robot.mousePress(InputEvent.BUTTON1_MASK);
		return 0;
	}

	/**
	 *
	 * 0=OK 1=other case
	 *
	 * @return int
	 */
	public int releaseLeft() {

		robot.mouseRelease(InputEvent.BUTTON1_MASK);
		return 0;
	}

	/**
	 *
	 * 0=OK 1=other case
	 *
	 * @return int
	 */
	public int pressRight() {
		robot.mousePress(InputEvent.BUTTON3_MASK);
		return 0;
	}

	/**
	 *
	 * 0=OK 1=other case
	 *
	 * @return int
	 */
	public int releaseRigth() {
		robot.mouseRelease(InputEvent.BUTTON3_MASK);
		return 0;
	}

	/**
	 *
	 * 0=OK 1=other case
	 *
	 * @return int
	 */
	public int pressMiddle() {
		robot.mousePress(InputEvent.BUTTON3_MASK);
		return 0;
	}

	/**
	 *
	 * 0=OK 1=other case
	 *
	 * @return int
	 */
	public int releaseMiddle() {
		robot.mouseRelease(InputEvent.BUTTON2_MASK);
		return 0;
	}

	@Override
	public int getXCursor() {
		// TODO Auto-generated method stub
		return x;
	}

	@Override
	public int getYCursor() {
		// TODO Auto-generated method stub
		return y;
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		x = e.getX();
		y = e.getY();
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		x = e.getX();
		y = e.getY();
	}

	@Override
	public int scroll(int scr) {
		// TODO Auto-generated method stub
		this.robot.mouseWheel(-scr);
		return 0;
	}

}
